package com.xwtec.proxy.v5;

import java.util.Random;

/**
 * @Author: zt
 * @Date: 2021/3/2 22:01
 *
 * 利用聚合，静态代理，可以实现嵌套
 */
public class Tank implements Movable {
    @Override
    public void move() {

        System.out.println("Tank Move claclacla.....");
        try {
            Thread.sleep(new Random().nextInt(4000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
}

interface Movable{
    void move();
}

//时间代理
class TankTimeProxy implements Movable {
    Movable m ;

    public TankTimeProxy(Movable m) {
        this.m = m;
    }

    @Override
    public void move() {
        long start  = System.currentTimeMillis();
        m.move();
        long end = System.currentTimeMillis();
        System.out.println(end-start);

    }


}

//日志代理
class TankLogProxy implements Movable {
    Movable m ;

    public TankLogProxy(Movable m) {
        this.m = m;
    }

    @Override
    public void move() {
        System.out.println("Tank start moving");
        m.move();
        System.out.println("Tank end moving");

    }

    public static void main(String[] args) {
        new TankLogProxy(new TankTimeProxy(new Tank())).move();
    }


}